﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Core;

namespace GSLMS.WebAPP.Areas.System.Controllers.Role
{
    public class RoleController : AdminBaseController
    {

        protected override string navTabId
        {
            get { return "System_Role"; }
        }
        //
        // GET: /Roles/Default/

        public ActionResult Index(FormCollection collection)
        {
            return View(GetList(collection));
        }

        //
        // GET: /Roles/Default/Details/5

        public ActionResult Details(long id)
        {
            return View();
        }

        //
        // GET: /Roles/Default/Create

        public ActionResult Create(long? id)
        {
            GSLMS.Entity.Role model = new GSLMS.Entity.Role()
            {
                Creator = CurentUser.UserName,
                CreateTime = DateTime.Now
            };

            

            if (id > 0)
            {
                model = serviceRoles.GetByID((long)id);
            }

            //List<VRolePurview> roles = serviceRoles.GetVRolePurviewByID(id);
            //ViewData["Roles"] = roles;
            ViewData["Modules"] = serviceModule.GetAll();
            return View(model);
        }

        //
        // POST: /Roles/Default/Create

        [HttpPost]
        public string Create(GSLMS.Entity.Role model)
        {
            try
            {
                var retult = false;
                model.CreateTime = model.CreateTime ?? DateTime.Now;
                model.Creator = string.IsNullOrEmpty(model.Creator) ? CurentUser.UserName : model.Creator;
                retult = model.ID > 0 ? serviceRoles.Update(model) : serviceRoles.Add(model);
                if (retult)
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        [HttpPost]
        public string Delete(long id)
        {
            try
            {
                if (serviceRoles.Delete(id))
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";
        }

        protected List<GSLMS.Entity.Role> GetList(FormCollection collection)
        {
            var searchParma = new GSLMS.Entity.Role();

            searchParma = base.SearchPara<GSLMS.Entity.Role>(collection, searchParma);

            var roleList=serviceRoles.GetList(searchParma);

            if(searchParma!=null&&searchParma.Name!=null)
              ViewData["RoleName"] = searchParma.Name.Trim();

            ViewData["RowCount"] = searchParma.RowCount;

            return roleList;
        }

    }
}
